package tjs.study.notes.eureka.clienta.order.service;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tjs.study.notes.eureka.clienta.order.OrderApplication;

/**
 * Created by Nicky.Tang on 2021/10/31 4:52 下午
 *
 * @since 02.12.10
 */
@Service
@Transactional
public class OrederService {
    private static final Logger logger = LoggerFactory.getLogger(OrederService.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @LcnTransaction
    public String newOrder(String userId, Integer sleepTime) {
        jdbcTemplate.execute("UPDATE user_test ut SET ut.balance = ut.balance + 1 WHERE ut.id = 2;");
        logger.warn("【order微服务.{}】2. 开始创建订单{}", OrderApplication.INSTANCE_ID, userId);
        if (sleepTime != null) {
            try {
                Thread.currentThread().sleep(sleepTime * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (userId != null && userId.indexOf("throw Excepetion") != -1) {
            throw new RuntimeException("测试抛出异常-回滚。。。");
        }
        return MessageFormatter.format("【order微服务.{}】2. 开始创建订单 userid={}", OrderApplication.INSTANCE_ID, userId).getMessage();
    }
}
